/*
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.core.resources;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.p4eclipse.core.PerforceException;
import net.sourceforge.p4eclipse.core.PerforceSynchronizer;
import net.sourceforge.p4eclipse.core.syncinfo.FolderSyncInfo;
import net.sourceforge.p4eclipse.core.syncinfo.ResourceSyncInfo;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.team.core.TeamException;

/**
  * @version 	1.0
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class LocalFolder extends LocalResource
{
	/**
	 * Constructor for LocalFolder.
	 * @param ioResource
	 */
	LocalFolder(IContainer folder)
	{
		super(folder);
	}

	public boolean hasRemote()
	{
		try
		{
			return PerforceUIPlugin.getSynchronizer().getFolderSync((IContainer)toResource()) != null;
		}
		catch (PerforceException e)
		{
			PerforceUIPlugin.getPlugin().log(e.getStatus());
			return false;
		}
	}
	
	public LocalFile[] getFiles()
		throws TeamException
	{
		try
		{
			IContainer container = (IContainer)toResource();
			IResource[] members = container.members();
			
			List result = new ArrayList();
			for (int i = 0, length = members.length; i < length; i++)
			{
				if (members[i].getType() == IResource.FILE)
					result.add(new LocalFile((IFile)members[i]));
			}
			
			return (LocalFile[])result.toArray(new LocalFile[0]);
		}
		catch (CoreException e)
		{
			throw new TeamException(e.getStatus());
		}
	}
	
	public LocalFolder[] getFolders()
		throws TeamException
	{
		try
		{
			IContainer container = (IContainer)toResource();
			IResource[] members = container.members();
			
			List result = new ArrayList();
			for (int i = 0, length = members.length; i < length; i++)
			{
				if (members[i].getType() != IResource.FILE)
					result.add(new LocalFolder((IContainer)members[i]));
			}
			
			return (LocalFolder[])result.toArray(new LocalFolder[0]);
		}
		catch (CoreException e)
		{
			throw new TeamException(e.getStatus());
		}
	}	
	
	public boolean isCheckedOut()
	{
		return false;
	}	

	public boolean isDirty()
	{
		return false;
	}	

	public boolean isOutOfDate()
	{
		return false;
	}
	
	public FolderSyncInfo getFolderSync()
	{
		try
		{
			return PerforceSynchronizer.getInstance().getFolderSync((IContainer)toResource());
		}
		catch (PerforceException e)
		{
			PerforceUIPlugin.getPlugin().log(e.getStatus());
		}
		
		return null;
	}
	
	public ResourceSyncInfo getSyncInfo()
	{
		return null;
	}
}
